package com.study.fetch.ths;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.NumberUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.study.fetch.ths.RkDemo.RkData.RkDTO;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhuzi
 * 2025/1/5 12:40
 */
public class RkDemo {
    /**
     * 计算收益率
     *
     * @param end
     * @param start
     * @return
     */
    public static BigDecimal syl(BigDecimal end, BigDecimal start) {
        if (start == null) {
            return null;
        }
        BigDecimal div = NumberUtil.div(end, start);
        BigDecimal sub = NumberUtil.sub(div, 1);
        return NumberUtil.mul(sub, 100).setScale(3, RoundingMode.HALF_UP);
    }

    interface RkData {
        /**
         * 日K数据
         */
        @Data
        @Accessors(chain = true)
        @ExcelIgnoreUnannotated
        class RkDTO {
            private String code;
            @ExcelProperty(value = "日期")
            private LocalDate date;
            @ExcelProperty(value = "开盘")
            private BigDecimal kp;
            @ExcelProperty(value = "最高")
            private BigDecimal zg;
            @ExcelProperty(value = "最低")
            private BigDecimal zd;
            @ExcelProperty(value = "收盘")
            private BigDecimal sp;
            @ExcelProperty(value = "涨幅")
            private BigDecimal zf;
            /**
             * 最高涨幅
             */
            @ExcelProperty(value = "最高涨幅")
            private BigDecimal zfg;
            /**
             * 最低涨幅
             */
            @ExcelProperty(value = "最低涨幅")
            private BigDecimal zfd;
            /**
             *
             */
            @ExcelProperty(value = "振幅")
            private BigDecimal zhenfu;

            public static RkDTO build(String dataStr) {

                String[] split = StringUtils.split(dataStr, ",");
                LocalDate date = LocalDateTimeUtil.parseDate(split[0], "yyyyMMdd");
                return new RkDTO().setDate(date).setKp(new BigDecimal(split[1]))
                        .setZg(new BigDecimal(split[2]))
                        .setZd(new BigDecimal(split[3]))
                        .setSp(new BigDecimal(split[4]));
            }


        }
    }


    @Test
    public void kzzRk() {
        String utf8String = FileUtil.readUtf8String("/Users/zhuzi/Downloads/kzz日K.txt");
        JSONObject jsonObject = JSON.parseObject(utf8String);
        String string = jsonObject.getString("data");
        System.out.println(string);
        List<RkDTO> collect = Arrays.stream(string.split(";")).map(RkDTO::build).sorted(Comparator.comparing(RkDTO::getDate).reversed()).collect(Collectors.toList());

        for (int i = 0; i < collect.size() - 1; i++) {
            RkDTO rkDTO = collect.get(i);
            RkDTO rkDTO1 = collect.get(i + 1);

            BigDecimal sp = rkDTO1.getSp();

            BigDecimal sp1 = rkDTO.getSp();
            BigDecimal zd = rkDTO.getZd();
            BigDecimal zg = rkDTO.getZg();
            rkDTO.setZf(syl(sp1, sp));
            rkDTO.setZfg(syl(zg, sp));
            rkDTO.setZfd(syl(zd, sp));

            rkDTO.setZhenfu(syl(zg, zd));
            System.out.println(rkDTO);

        }
        EasyExcel.write("/Users/zhuzi/Downloads/同花顺可转债日K.xlsx", RkDTO.class).sheet("可转债")

                .doWrite(collect);
    }

    @Test
    public void sout() {
        String str = "\\u53ef\\u8f6c\\u503a\\u6307\\u6570";
        str = "\"name\":\"\\u53ef\\u8f6c\\u503a\\u6307\\u6570\",\"data\":\"20200508,1005.350,1007.532,1003.978,1005.967";
        System.out.println(str);
        //System.out.println(fromUnicode(str));
        System.out.println(unicodeToStr(str));
    }

    public static String fromUnicode(String unicode) {
        StringBuilder str = new StringBuilder();
        String[] hex = unicode.split("\\\\u");
        System.out.println("\\\\u");
        for (int i = 1; i < hex.length; i++) {
            int data = Integer.parseInt(hex[i], 16);
            str.append((char) data);
        }
        return str.toString();
    }

    /**
     * description: 字符串  转 Unicode --- 使用工具类实现一键转换
     *
     * @param str
     * @return String
     * @version v1.0
     * @author w
     * @date 2021年4月21日 上午10:21:55
     */
    public static String strToUnicode(String str) {
        return StringEscapeUtils.escapeJava(str);
    }

    /**
     * description:  Unicode 转字符串  --- 使用工具类实现一键转换
     *
     * @param unicode
     * @return String
     * @version v1.0
     * @author w
     * @date 2021年4月21日 上午10:21:02
     */
    public static String unicodeToStr(String unicode) {
        return StringEscapeUtils.unescapeJava(unicode);
    }
}
